# -*- coding: utf-8 -*-

from __future__ import with_statement

from flask import g
from google.appengine.ext import db
from datetime import datetime
from werkzeug.urls import url_decode
from urlparse import urlparse

from dicthash.for_testing import TestGoogleDataStore, in_namespace, captured_templates
from dicthash.utils import Bunch

from lenta.code.backends.working.models import Lenta, Note
from application.app import app

def set_user_for_testing():
    """
    Mock for app's set_user before request handler.
    """
    g.user = Bunch(name="foo", openid="foo@bar.com")

app.before_request_funcs.get(None)[0] = set_user_for_testing


class TestEditLenta(TestGoogleDataStore):
    """
    Testing `lenta.code.views.lenta.edit_lenta`
    """
    def setUp(self):
        """ """
        super(TestEditLenta, self).setUp()
        
        with in_namespace("foo"):
            self.lenta = Lenta.new(name=u"Дневник ебанько", desc=u"Эту книгу ждали 8 лет...")
            self.lenta.put()
            
    def test_setUp(self):
        """ """
        lentas = Lenta.all(namespace="foo").fetch(10)
        self.assertEqual(len(lentas), 1)
        self.assertEqual(lentas[0].name, u"Дневник ебанько")
        
    def test_ok(self):
        """ """
        with captured_templates(app) as tmpl_info:
            POST = dict(
                name="Дневник ебанько",
                desc="Всем похуй на этот высер графомана."
            )
            res = app.test_client().post("/lenta/%s/edit" % self.lenta.id, data=POST)
            
            self.assertEqual(res.status_code, 200)
            self.assertEqual(tmpl_info[0][0], "/lenta/lenta/edit_lenta_ok.html")
            self.assertTrue('lenta' in tmpl_info[0][1])
            